package com.ricervcvcer.order.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ricervcvcer.order.client.GoodsClient;
import com.ricervcvcer.order.entity.OrderDetailEntity;
import com.ricervcvcer.order.entity.TbSpecParam;
import com.ricervcvcer.order.service.OrderDetailService;
import com.ricervcvcer.order.util.PageUtils;
import com.ricervcvcer.order.util.R;
import com.ricervcvcer.order.vo.SkuParamVO;
import com.ricervcvcer.order.vo.SpecParamVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 订单详情表
 *
 * @author wlh
 * @email wlhstudy@163.com
 * @date 2022-03-04 10:10:57
 */
@RestController
@RequestMapping("/orderdetail")
public class OrderDetailController {
    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private GoodsClient client;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderDetailService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		OrderDetailEntity orderDetail = orderDetailService.getById(id);
        return R.ok().put("orderDetail", orderDetail);
    }

    /**
     * 根据订单信息查询
     */
    @RequestMapping("/infos/{id}")
    public R infos(@PathVariable("id") String id){
        QueryWrapper qw=new QueryWrapper();
        qw.eq("order_id",id);
        List<OrderDetailEntity> orderDetails = orderDetailService.list(qw);

        // 解析结果
        List<SkuParamVO> collect = orderDetails.stream().map(item -> {
            SkuParamVO skuParamVO = new SkuParamVO();
            BeanUtils.copyProperties(item, skuParamVO);
            List<SpecParamVO> specParamVOList = new ArrayList<>();
            Map<String, Object> param = JSON.parseObject(item.getOwnSpec());
            for (String key : param.keySet()) {
                //keySet获取map集合key的集合  然后在遍历key即可
                SpecParamVO spvo = new SpecParamVO();
                TbSpecParam specParam = client.getByid(Long.parseLong(key)).getData();
                BeanUtils.copyProperties(specParam, spvo);
                spvo.setValue(param.get(key));
                specParamVOList.add(spvo);
            }
            skuParamVO.setSpecParamVO(specParamVOList);
            return skuParamVO;
        }).collect(Collectors.toList());
        return R.ok().put("orderDetail", collect);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody OrderDetailEntity orderDetail){
		orderDetailService.save(orderDetail);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody OrderDetailEntity orderDetail){
		orderDetailService.updateById(orderDetail);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		orderDetailService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
